package com.ytjj.qmyx.users.controller;

import com.ytjj.common.controller.BaseController;
import com.ytjj.qmyx.users.model.DynamicIncomeDto;
import com.ytjj.qmyx.users.model.UserBalanceDto;
import com.ytjj.qmyx.users.service.UserBalanceService;
import com.ytjj.qmyx.users.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/account")
@Slf4j
public class UserAccountController extends BaseController {
    @Autowired
    private UsersService usersService;
    @Resource
    private UserBalanceService userBalanceService;
    /**
     * 添加余额
     * @param balance 增加金额值
     * @param userId  用户id
     * @param type    增加类型
     * @return
     */
    @GetMapping("/addBalance")
    public Integer addBalance(@RequestParam("balance") BigDecimal balance,
                              @RequestParam("userId") Integer userId,
                              @RequestParam("type") String type) {
        return usersService.addBalance(balance, userId, type);
    }

    /**
     * 减余额
     * @param balance 扣减金额值
     * @param userId  用户id
     * @param type    扣减类型
     * @return
     */
    @GetMapping("/subBalance")
    public Integer subBalance(@RequestParam("balance") BigDecimal balance,
                              @RequestParam("userId") Integer userId,
                              @RequestParam("type") String type) {

        return usersService.subBalance(balance, userId, type);
    }

    /**
     * 用户金额加减
     */
    @PostMapping("update")
    public void updateMoney(@RequestBody UserBalanceDto userBalanceDto) {
        userBalanceService.update(userBalanceDto);
    }

    /**
     * 查询用户每天收入信息
     * @param userId 用户id
     * @param type   ADD/SUB
     * @param source 收入来源
     * @return
     */
    @GetMapping("/incomeItem")
    public List<DynamicIncomeDto> queryUsersIncomeItemByUserIdAndTypeAndSource(@RequestParam("userId") Integer userId,
                                                                               @RequestParam("type") String type,
                                                                               @RequestParam("source") String source,
                                                                               @RequestParam("timeType") Integer timeType) {
        List<DynamicIncomeDto> list = userBalanceService.queryUsersIncomeItemByUserIdAndTypeAndSource(userId, type, source, timeType);
        return list;
    }
}
